package com.naizhuai.ui.user;


import com.naizhuai.AppContext;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.network.Config;
import com.naizhuai.network.RetrofitServerFactory;
import com.naizhuai.network.apistore.UserDataApiRequest;
import com.naizhuai.network.apistore.UserManageDataApiStore;
import com.naizhuai.network.upload.FileUpload;
import com.naizhuai.ui.entity.ImageBean;
import com.naizhuai.ui.entity.ImgBean;
import com.naizhuai.ui.entity.User.AppMessage;
import com.naizhuai.ui.entity.User.AppMessageList;
import com.naizhuai.ui.entity.User.CateGoryMsg;
import com.naizhuai.ui.entity.User.Simulation;
import com.naizhuai.ui.entity.User.SimulationData;
import com.naizhuai.ui.entity.User.UserRank;
import com.naizhuai.ui.entity.UserUpdate;
import com.naizhuai.ui.entity.setting.ThankDetailsData;
import com.naizhuai.util.RxUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import rx.Observable;


/**
 * 管理接口的实现类
 * Created by code_han on 2018/1/25.
 */

public class UserManageDataApiStoreIml implements UserManageDataApiStore {

    private static UserManageDataApiStoreIml INSTANCE = null;

    public static UserManageDataApiStoreIml getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new UserManageDataApiStoreIml();
        }
        return INSTANCE;
    }

    @Override
    public Observable<String> sendSms(String phoneNumber, String type) {
        return createBaseClass().sendSms(phoneNumber, type)
                .compose(RxUtils.applySchedulers());
    }

    @Override

    public Observable<UserLogin> userRegister(String truename, String phone,
                                           String code, String password,String mail) {
        return createBaseClass().userRegister(truename, phone, code, password, mail).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<UserLogin> userLogin(String phoneNumber, String password) {
        return createLoginBaseClass()
                .userlogin(phoneNumber, password)
                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<PersonUserBean> getuserinfo() {
        return createBaseClass().getUserInfo().compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<ThankDetailsData>> getThankCard() {
        return createBaseClass().getThankCard().compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<PersonUserBean> searchUserInfo(String searchId) {
        return createBaseClass().searchUserInfo(searchId).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<ImgBean> uploadImg(String path) {
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("module", FileUpload.adp);
        return createBaseClass().uploadFileWithPartMap(stringMap, FileUpload.getPart(path))
                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<ImgBean>> uploadImgs(List<String> filepaths) {
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("module", FileUpload.adp);
        List<MultipartBody.Part> parts = new ArrayList<>();
        for (String path : filepaths) {
            MultipartBody.Part part = FileUpload.getParts(path);
            parts.add(part);
        }
        return createBaseClass().uploadFileWithPartMaps(stringMap, parts)
                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<UserUpdate> userUpdate(String truename,
                                             String thumb, String email) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("truename", truename);
        paramMap.put("thumb", thumb);
        paramMap.put("email", email);
        return createBaseClass().userupdate(paramMap).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> resetpassword(String phone, String msgCode, String password, String rePassword) {
        Map paramMap = new HashMap();
        paramMap.put("phone", phone);
        paramMap.put("msgCode", msgCode);
        paramMap.put("password", password);
        paramMap.put("rePassword", rePassword);
        return createBaseClass().resetPassword(paramMap).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<UserRank> getPerson_Rank(String key, String dateValue) {
        return createBaseClass().getPerson_Rank(key, dateValue).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<UserRank> getGroup_Rank(String key, String dateValue) {
        return createBaseClass().getGroup_Rank(key, dateValue).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<SimulationData> getComPanySimalationData() {
        return createBaseClass().getComPanySimalationData().compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> setComPanySimalationData(String effectData) {
        return createBaseClass().setComPanySimalationData(effectData).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<AppMessage>> getAppMessage() {
        return createBaseClass().getAppMessage().compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<CateGoryMsg> getAppMessageList(String categoryId) {
        return createBaseClass().getAppMessageList(categoryId).compose(RxUtils.applySchedulers());
    }

    private UserDataApiRequest createBaseClass() {
        return RetrofitServerFactory.create(AppContext.getInstance(), UserDataApiRequest.class, Config.TEST_ACTION_BASE_URL);
    }

    private UserDataApiRequest createLoginBaseClass() {
        return RetrofitServerFactory.create(AppContext.getInstance(), UserDataApiRequest.class, Config.LOGIN_BASEURL);
    }
}
